package serv

import (
	"context"
	"fmt"
	"generate_grom_mode_and_dao/common/helper"
	"generate_grom_mode_and_dao/service/genertategrom/api/internal/svc"
	"generate_grom_mode_and_dao/service/genertategrom/api/model"
	"github.com/tal-tech/go-zero/core/logx"
	"os"
	"text/template"

	//"io/ioutil"
	"strings"
)

type GenerateTestService interface {
	Create() error
}

type defaultGenerateTestService struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGenerateTestService(ctx context.Context, svcCtx *svc.ServiceContext) *defaultGenerateTestService {
	return &defaultGenerateTestService{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (s defaultGenerateTestService) Create() error {

	info, err := s.svcCtx.DatabaseDao.GetAllColumnInfo("")

	toString := helper.StringHelperObject.ToString(info)

	fmt.Println(err)
	fmt.Println(toString)
	//获取数据结构里面的数据
	tableMap := make(map[string][]model.InformationSchemaColumns)
	for _, column := range *info {
		tableName := column.TableName
		if _, ok := tableMap[tableName]; !ok {
			tableMap[tableName] = make([]model.InformationSchemaColumns, 0)
		}
		tableMap[tableName] = append(tableMap[tableName], column)
	}

	funcMap := template.FuncMap{
		"upperCamelCase": helper.StringHelperObject.UpperCamelCase,
		"lowerCamelCase": helper.StringHelperObject.LowerCamelCase,
	}
	t, err := template.New(s.svcCtx.Config.MysqlGenerateXorm.TplName).Funcs(funcMap).ParseFiles(s.svcCtx.Config.MysqlGenerateXorm.TplFile)
	if err != nil {
		fmt.Println("parse file err:", err)
		return err
	}
	os.RemoveAll(s.svcCtx.Config.MysqlGenerateXorm.SavePath)
	for table, columns := range tableMap {
		if _, err := os.Stat(s.svcCtx.Config.MysqlGenerateXorm.SavePath); os.IsNotExist(err) {
			os.Mkdir(s.svcCtx.Config.MysqlGenerateXorm.SavePath, 0777)
			os.Chmod(s.svcCtx.Config.MysqlGenerateXorm.SavePath, 0777)
		}
		builder := new(strings.Builder)
		builder.WriteString(s.svcCtx.Config.MysqlGenerateXorm.SavePath)
		builder.WriteString("/")
		if s.svcCtx.Config.MysqlGenerateXorm.Lang == "go" {
			builder.WriteString(table)
			if s.svcCtx.Config.MysqlGenerateXorm.TargetType == "repository" {
				builder.WriteString("_repository")
			}
		}

		//else if s.svcCtx.Config.MysqlGenerateXorm.Lang == "java" {
		//	if s.svcCtx.Config.MysqlGenerateXorm.TargetType == "entity" {
		//		fileSb.WriteString(helper.StringHelperObject.UpperCamelCase(table))
		//		fileSb.WriteString("Entity")
		//	} else if s.svcCtx.Config.MysqlGenerateXorm.TargetType == "model" {
		//		fileSb.WriteString(helper.StringHelperObject.UpperCamelCase(table))
		//	} else if s.svcCtx.Config.MysqlGenerateXorm.TargetType == "repository" {
		//		fileSb.WriteString(helper.StringHelperObject.UpperCamelCase(table))
		//		fileSb.WriteString("EntityRepository")
		//	}
		//}
		builder.WriteString(".")
		builder.WriteString(s.svcCtx.Config.MysqlGenerateXorm.Lang)
		f, err := os.OpenFile(builder.String(), os.O_CREATE|os.O_WRONLY, 0666)
		defer f.Close()
		if err != nil {
			fmt.Println("can not create output file,err:", err)
			return err
		}
		//传递配置信息
		serviceConfig := NewConfigService(s.ctx, s.svcCtx)
		serviceConfig.TableName = table
		serviceConfig.Readonly = s.svcCtx.Config.MysqlGenerateXorm.Readonly
		serviceConfig.PackageName = s.svcCtx.Config.MysqlGenerateXorm.PackageName
		serviceConfig.Columns = columns

		columns2s := make([]model.InformationSchemaColumns2, 0)
		for _, i2 := range columns {
			var tmp model.InformationSchemaColumns2
			tmp.ColumnName = i2.ColumnName
			tmp.ColumnComment = i2.ColumnComment
			columns2s = append(columns2s, tmp)
		}

		serviceConfig.Columns2 = columns2s

		if err := t.Execute(f, &serviceConfig); err != nil {
			fmt.Println("There was an error:", err.Error())
		}

		//if err := t.Execute(f, &Config{
		//	TableName: table,
		//	Readonly: s.svcCtx.Config.MysqlGenerateXorm.Readonly,
		//	PackageName: s.svcCtx.Config.MysqlGenerateXorm.PackageName,
		//	Columns: columns,
		//}); err != nil {
		//	fmt.Println("There was an error:", err.Error())
		//}
	}
	return nil
}
